﻿/*
 * Copyright (C) 2011 Kamran Zafar
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace org.kamranzafar.hotfusion.gui
{
	public partial class FileSettingsForm : Form
	{
		private BindEntry.FileEntry fileSettings;
		private bool remoteFile;

		public FileSettingsForm(BindEntry.FileEntry fileSettings)
		{
			InitializeComponent();
			this.fileSettings = fileSettings;

			init();
		}

		public FileSettingsForm(BindEntry fileSettings, bool remoteFile)
		{
			//FileSettingsForm(fileSettings);
			this.remoteFile=remoteFile;
		}

		
		private void okBtn_Click(object sender, EventArgs e)
		{
			if (isValid()) {
				fileSettings = new BindEntry.FileEntry();
				fileSettings.FileName = fileTxt.Text;
				fileSettings.DestinationPath = destTxt.Text;
				fileSettings.RunOnExtract=runRBtn.Checked;
				fileSettings.RandomlyRename=renameRBtn.Checked;
				fileSettings.RenameFileTo="";
				fileSettings.AddToStartup=startupRBtn.Checked;
				fileSettings.AddToPath=addDestToPathChk.Checked;
				fileSettings.RegKey=regKeyTxt.Text;
				fileSettings.KeyType=(hkcuRbtn.Checked) ? 1 : 2;
				fileSettings.RandomKey=randomKeyChk.Checked;
				fileSettings.Compress=compressChk.Checked;
				fileSettings.Encrypt=encryptChk.Checked;
				
				if(encryptChk.Checked){
					fileSettings.Passphrase=passPhraseTxt.Text;
				}
				
				if(runRBtn.Checked){
					fileSettings.RunParams=(runParamsTxt.Text.Trim().Equals("") || runParamsTxt.Text.Trim().Equals("< Params >")) ? "" : runParamsTxt.Text;
				}
				
				
				this.Close();
			}
		}

		private void init() {
			toolTip1.SetToolTip(varBtn, "Special folders");
			toolTip1.SetToolTip(browseBtn, "Browse file");
			
			if (fileSettings != null)
			{
				fileTxt.Text = fileSettings.FileName;
				destTxt.Text = fileSettings.DestinationPath == null ? "" : fileSettings.DestinationPath;
				runRBtn.Checked = fileSettings.RunOnExtract;
				renameRBtn.Checked = fileSettings.RandomlyRename;
				startupRBtn.Checked = fileSettings.AddToStartup;
				addDestToPathChk.Checked = fileSettings.AddToPath;
				compressChk.Checked = fileSettings.Compress;
				encryptChk.Checked = fileSettings.Encrypt;
				
				if(fileSettings.Encrypt){
					passPhraseTxt.Text = fileSettings.Passphrase;
				}
				
				if(fileSettings.RunOnExtract){
					runParamsTxt.Text=fileSettings.RunParams;
				}
				
				if(fileSettings.AddToStartup){
					regKeyTxt.Text=fileSettings.RegKey;
					hkcuRbtn.Checked=(fileSettings.KeyType==1) ? true : false;
					hklmRbtn.Checked=(fileSettings.KeyType==2) ? true : false;
					randomKeyChk.Checked=fileSettings.RandomKey;
				}
			}
			
			changePassPhraseStyle(false);
			changeParamStyle(false);
		}

		private bool isValid() {
			if (fileTxt.Text.Equals("")) {
				MessageBox.Show("Please choose a file.");
				browseBtn.Focus();
				return false;
			}

			if (destTxt.Text.Trim().Equals("")) {
				MessageBox.Show("Please enter a destination path.");
				destTxt.Focus();
				return false;
			}
			
			if(startupRBtn.Checked && !randomKeyChk.Checked && regKeyTxt.Text.Trim().Equals("")){
				MessageBox.Show("Please enter a Reg Key or select Random key.");
				regKeyTxt.Focus();
				return false;
			}
			
			if(encryptChk.Checked
			   && (passPhraseTxt.Text.Trim().Equals("")
			       || passPhraseTxt.Text.Trim().Equals("< Passphrase >"))){
				MessageBox.Show("Please enter a passphrase.");
				passPhraseTxt.Focus();
				return false;
			}

			return true;
		}

		public BindEntry.FileEntry getFileSettings(){
			return fileSettings;
		}

		private void cancelBtn_Click(object sender, EventArgs e)
		{
			fileSettings = null;
			this.Close();
		}

		private void browseBtn_Click(object sender, EventArgs e)
		{
			if (DialogResult.OK != openFileDialog1.ShowDialog(this))
				return;

			fileTxt.Text = openFileDialog1.FileName;
		}

		private void varBtn_Click(object sender, EventArgs e)
		{
			DestinationFolderForm dff = new DestinationFolderForm();
			dff.ShowDialog(this);
			
			if(dff.DestPath!=null){
				if(DestinationPath.isSpecialPath(destTxt.Text)){
					string[] tmpdp=destTxt.Text.Split('}');
					tmpdp[0]="{"+dff.DestPath.Trim(new char[]{'{','}'});
					
					destTxt.Text=string.Join("}",tmpdp);
				}else{
					destTxt.Text=dff.DestPath + @"\" + destTxt.Text;
				}
			}
		}
		
		void RandomKeyChkCheckedChanged(object sender, EventArgs e)
		{
			if(randomKeyChk.Checked){
				regKeyTxt.Text="";
				regKeyTxt.Enabled=false;
			}else{
				regKeyTxt.Enabled=true;
			}
		}
		
		void PassPhraseTxtEnter(object sender, EventArgs e)
		{
			changePassPhraseStyle(true);
		}
		
		void changePassPhraseStyle(bool selected){
			if(passPhraseTxt.Text.Trim().Equals("") && !selected){
				passPhraseTxt.Text="< Passphrase >";
				passPhraseTxt.Font=new Font(this.Font, FontStyle.Italic);
				passPhraseTxt.ForeColor=Color.Gray;
			}else if(selected && passPhraseTxt.Text.Trim().Equals("< Passphrase >")){
				passPhraseTxt.Text="";
				passPhraseTxt.Font=new Font(this.Font, FontStyle.Regular);
				passPhraseTxt.ForeColor=Color.Black;
			}
		}
		
		void changeParamStyle(bool selected){
			if(runParamsTxt.Text.Trim().Equals("") && !selected){
				runParamsTxt.Text="< Params >";
				runParamsTxt.Font=new Font(this.Font, FontStyle.Italic);
				runParamsTxt.ForeColor=Color.Gray;
			}else if(selected && runParamsTxt.Text.Trim().Equals("< Params >")){
				runParamsTxt.Text="";
				runParamsTxt.Font=new Font(this.Font, FontStyle.Regular);
				runParamsTxt.ForeColor=Color.Black;
			}
		}
		
		void PassPhraseTxtLeave(object sender, EventArgs e)
		{
			changePassPhraseStyle(false);
		}
		
		void RunParamsTxtEnter(object sender, EventArgs e)
		{
			changeParamStyle(true);
		}
		
		void RunParamsTxtLeave(object sender, EventArgs e)
		{
			changeParamStyle(false);
		}
	}
}
